package cn.demomaster.quickui_sample.ui.activity.component;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.view.menu.OptionsMenu;
import com.demomaster.quickcompat.widget.base.QuickGravity;
import com.demomaster.quickcompat.widget.dialog.QDMulSheetDialog;
import com.demomaster.quickcompat.widget.dialog.QuickDialog;
import com.demomaster.quickcompat.widget.dialog.QuickInputDialog;
import com.demomaster.quickcompat.widget.dialog.QuickShareDialog;
import com.demomaster.quickcompat.widget.dialog.QuickSheetDialog;
import com.demomaster.quickcompat.widget.dialog.adapter.ShareAdapter;
import com.demomaster.quickcompat.widget.toast.QdToast;

import java.util.ArrayList;
import java.util.List;

import cn.demomaster.quickpicturer.helper.PhotoHelper;
import cn.demomaster.quickrouter.fragment.QuickFragment;
import cn.demomaster.quickui.annotation.ActivityPager;
import cn.demomaster.quickui.annotation.ResType;
import cn.demomaster.quickui_sample.R;

@ActivityPager(name = "对话框", preViewClass = TextView.class, resType = ResType.Resource)//, iconRes = R.mipmap.ic_dialog)
public class DialogActivity extends QuickFragment {
    private int backgroundRadio = 20;
    private ListView mListView;

    @Override
    public View onGenerateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.activity_qdialog, null);
        return mView;
    }

    @Override
    public String getTitle() {
        return "对话框";
    }

    @Override
    public void initView(View rootView) {
        getActionBarTool().setRightOnClickListener(v -> {
            //getOptionsMenu().show();
            showSheetMenu();
        });

        mListView = findViewById(R.id.listview);
        String[] listItems = new String[]{
                "提示框（不带内容）",
                "提示框（带内容）",
                "对话框（菜单类型）",
                "对话框（单选）",
                "对话框（多选）",
                "带 Checkbox 的消息确认框",
                "单选菜单类型对话框",
                "多选菜单类型对话框",
                "多选菜单类型对话框(item 数量很多)",
                "带输入框的对话框",
                "高度适应键盘升降的对话框",
                "分享",
                "带radiobutton的单选窗",
        };
        /*List<String> data = new ArrayList<>();
        Collections.addAll(data, listItems);*/
        mListView.setAdapter(new ArrayAdapter<>(mContext, R.layout.simple_list_item, listItems));
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0://简单提示框(带标题)
                    showMessage1();
                    break;
                case 1:
                    showMessageWithButton();
                    break;
                case 2://菜单类型对话框
                    showMenuDialog();
                    break;
                case 3://单选菜单类型对话框
                    showMulMenuDialog1();
                    break;
                case 4://多选菜单类型对话框
                    showMulMenuDialog();
                    break;
                case 5://高度适应键盘升降的对话框
                    showInputDialog();
                    break;
                case 6://高度适应键盘升降的对话框
                    showInputDialog2();
                    break;
                case 7://高度适应键盘升降的对话框
                    showShareDialog();
                    break;
            }
        });
    }

    private void showInputDialog() {
        QuickInputDialog quickInputDialog = new QuickInputDialog.Builder(mContext)
                //.setHint("请输入密码")
                .setBackgroundRadius(backgroundRadio)
                .setGravity(QuickGravity.RIGHT)
                .create();
        quickInputDialog.bindView(R.id.tv_title,"连接wifi");
        quickInputDialog.bindView(R.id.tv_left,"连接", (dialog, view, tag) -> {
                    String str = ((TextView)quickInputDialog.findViewById(R.id.et_input)).getText().toString();
                    Toast.makeText(mContext, "input = " + str, Toast.LENGTH_SHORT).show();
                    //连接返回editview的value
                }).bindView(R.id.tv_right,"取消");
        quickInputDialog.show();
    }

    private void showInputDialog2() {
        QuickInputDialog inputDialog = new QuickInputDialog.Builder(mContext)
                //.setHint("请输入密码")
                //.setBackgroundRadius(backgroundRadio)
                //.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD)

                .setGravity(QuickGravity.RIGHT)
                .create();
        inputDialog.bindView(R.id.tv_title, "连接wifi")
                .bindView(R.id.tv_left, "连接", (dialog, view, tag) -> {
                    String str = ((TextView)inputDialog.findViewById(R.id.et_input)).getText().toString();
                    Toast.makeText(mContext, "input = " + str, Toast.LENGTH_SHORT).show();
                    Toast.makeText(mContext, "input = " + tag, Toast.LENGTH_SHORT).show();
                    //连接返回editview的value
                })
                .bindView(R.id.tv_right, "取消")
                .show();
    }

    public OptionsMenu optionsMenu;

    //获取自定义菜单
    public OptionsMenu getOptionsMenu() {
        if (optionsMenu == null) {
            optionsMenu = new OptionsMenu(new OptionsMenu.Builder(mContext));
        }
        return optionsMenu;
    }

    private OptionsMenu.Builder optionsMenubuilder;

    //获取自定义菜单
    public OptionsMenu.Builder getOptionsMenuBuilder() {
        if (optionsMenubuilder == null) {
            optionsMenubuilder = new OptionsMenu.Builder(mContext);
        }
        return optionsMenubuilder;
    }

    private void showSheetMenu() {
        String[] menus = {"i1", "item2", "item3"};
        QuickSheetDialog dialog = new QuickSheetDialog.Builder(mContext)
                .setData(menus)
                .setGravity(QuickGravity.BOTTOM)
                .setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                .create();
        dialog.show();
    }

    QuickShareDialog shareDialog;

    private void showShareDialog() {
        if (shareDialog != null) {
            shareDialog.dismiss();
        }
        ArrayList<ShareAdapter.ShareModel> spinners = new ArrayList<>();
        String[] menus = new String[]{"微信", "微博", "朋友圈", "QQ空间"};// {"item1", "item2", "item3"};
        for (int i = 0; i < menus.length; i++) {
            ShareAdapter.ShareModel shareModel = new ShareAdapter.ShareModel();
            shareModel.setName("" + menus[i]);
            shareModel.setImgResId(R.mipmap.meizi);
            spinners.add(shareModel);
        }
        //menus = spinners.toArray(menus);
        shareDialog = new QuickShareDialog.Builder(mContext)
                .setContentView(com.demomaster.quickcompat.R.layout.quick_dialog_share_default)
                .setData(menus)
                .setOrientation(LinearLayout.HORIZONTAL)
                .setGravity(QuickGravity.BOTTOM)
                .setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                .create();
        shareDialog.setSheetAdapter(new ShareAdapter(mContext, spinners));
        shareDialog.show();
    }

    QuickDialog radioButtonDialog;

    private void showRadioButtonDialog() {
        if (radioButtonDialog == null) {
            radioButtonDialog = new QuickDialog.Builder(mContext)
                    .setContentView(R.layout.radio_button_dialog_layout)
                    .create();
            radioButtonDialog.bindView(R.id.tv_title, "确定要发送吗？");
            ArrayList<ShareAdapter.ShareModel> spinners = new ArrayList<>();
            String[] menus = new String[]{"微信", "微博", "朋友圈", "QQ空间"};// {"item1", "item2", "item3"};
            for (int i = 0; i < menus.length; i++) {
                ShareAdapter.ShareModel shareModel = new ShareAdapter.ShareModel();
                shareModel.setName("" + menus[i]);
                shareModel.setImgResId(R.mipmap.meizi);
                spinners.add(shareModel);
            }
            //menus = spinners.toArray(menus);
            shareDialog = new QuickShareDialog.Builder(mContext)
                    .setContentView(com.demomaster.quickcompat.R.layout.quick_dialog_share_default)
                    .setData(menus)
                    .setOrientation(LinearLayout.HORIZONTAL)
                    .setGravity(QuickGravity.BOTTOM)
                    .setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
                    .create();
            shareDialog.setSheetAdapter(new ShareAdapter(mContext, spinners));
        }
        shareDialog.show();
    }

    // ================================ 生成不同类型的对话框

    private void showMessage1() {
        QuickDialog quickDialog = new QuickDialog.Builder(mContext)
                .setContentView(com.demomaster.quickcompat.R.layout.quick_dialog_layout)
                .create();
        quickDialog.bindView(R.id.tv_title, "标题？")
                .bindView(R.id.tv_left, "确定")
                .bindView(R.id.tv_right, "取消");
        quickDialog.show();
    }

    private void showMessageWithButton() {
        QuickDialog quickDialog = new QuickDialog.Builder(mContext)
                .setContentView(com.demomaster.quickcompat.R.layout.quick_dialog_layout)
                .create();
        quickDialog.bindView(R.id.tv_title, "标题？")
                .bindView(R.id.tv_message, "确定要发送吗？")
                .bindView(R.id.tv_left, "取消")
                .bindView(R.id.tv_right, "确定");

        //GroundGlassUtil glassUtil = new GroundGlassUtil(quickDialog.getContext());
        //qdDialog.getContentView().addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> QdLog.println("onLayoutChange left=" + left + ",top=" + top + ",right=" + right + ",bottom=" + bottom));

        //glassUtil.setTargetView(qdDialog.getContentLinearView());
        //glassUtil.setBackgroundView(mListView);
        //glassUtil.setRadius(100);
        //glassUtil.invalidate();
        quickDialog.show();
        //glassUtil.setTargetView(qdDialog.getContentLinearView());
    }

    private void showMenuDialog() {
        int count = 30;
        String[] menus = new String[count];
        for (int i = 0; i < count; i++) {
            menus[i] = "第" + i + "项";
        }

        QuickSheetDialog.Builder builder = new QuickSheetDialog.Builder(mContext);
        builder.setData(menus);
        QuickSheetDialog quickSheetDialog = builder.create();
        quickSheetDialog.setOnDialogActionListener(new QuickSheetDialog.OnDialogActionListener() {
            @Override
            public void onItemClick(QuickSheetDialog dialog, int position, List<String> data) {
                dialog.dismiss();
                QdToast.show(mContext, data.get(position));
            }
        });
        quickSheetDialog.show();
    }

    private void showMulMenuDialog1() {
        int count = 50;
        String[] menus = new String[count];
        for (int i = 0; i < count; i++) {
            menus[i] = "第" + i + "项";
        }
        new QDMulSheetDialog.Builder(mContext).setData(menus).setOnDialogActionListener((dialog, position, data) -> {
            dialog.dismiss();
            QdToast.show(mContext, data.get(position));
        }).create().show();
    }

    private void showMulMenuDialog() {
        int count = 80;
        String[] menus = new String[count];
        for (int i = 0; i < count; i++) {
            menus[i] = "第" + i + "项";
        }
        new QDMulSheetDialog.Builder(mContext)
                .setData(menus)
                .setOnDialogActionListener((dialog, position, data) -> {
                    dialog.dismiss();
                    QdToast.show(mContext, data.get(position));
                }).create().show();
    }

}
